package 华为OD机试真题2023.基础题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class HJ26_箱子之形摆放 {
    public static void main(String[] args) throws FileNotFoundException {
        //方法一：List
        Scanner in = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/26.txt"));
        MyUtil.START();
        String[] sArray = in.nextLine().split(" ");
        String str = sArray[0];
        int width = Integer.parseInt(sArray[1]);
        List<List<Character>> result = new ArrayList<>();
        for (int i = 0; i < width; i++) {
            result.add(new ArrayList<>());
        }
        for (int i = 0; i < str.length(); i++) {
            int index = i % width;
            if (i / width % 2 == 1) {
                index = width - 1 - index;
            }
            result.get(index).add(str.charAt(i));
        }
        for (int i = 0; i < width; i++) {
            for (Character a : result.get(i)) {
                System.out.print(a);
            }
            if (i != width - 1) {
                System.out.println();
            }
        }
        MyUtil.END();

        //方法二：用 Map+StringBuilder
        Scanner in2 = new Scanner(new File("src/main/java/华为OD机试真题2023/基础题/input/26.txt"));
        MyUtil.START();
//        String[] sArray2 = in2.nextLine().split(" ");
        String[] sArray2 = sArray;
        String str2 = sArray2[0];
        int width2 = Integer.parseInt(sArray2[1]);
        Map<Integer, StringBuilder> result2 = new HashMap<>();
        for (int i = 0; i < width; i++) {
            result2.put(i, new StringBuilder());
        }
        for (int i = 0; i < str.length(); i++) {
            int index = i % width2;
            if (i / width2 % 2 == 1) {
                index = width2 - 1 - index;
            }
            result2.get(index).append(str.charAt(i));
        }
        for (int i = 0; i < width; i++) {
            System.out.print(result2.get(i));
            if (i != width - 1) {
                System.out.println();
            }
        }
        MyUtil.END();
    }
}
